package com.example.fashion.android.view;

/**
 * Created by fashion on 2017/7/4.
 */

import android.content.Context;
import android.util.AttributeSet;
import android.view.View;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.example.fashion.android.R;
import com.example.fashion.android.base.BaseActivity.BaseActivity;


/**
 * 基类BaseActivity用到的TitleBar，用来返回页面和展示当前页内容的Title信息
 *
 * @author mingwei
 */
public class TitleBar extends RelativeLayout implements View.OnClickListener {

    private ImageView mBack;
    private TextView mTitle;
    private BaseActivity mContext;

    public TitleBar(Context context) {
        this(context, null);
    }

    public TitleBar(Context context, AttributeSet attrs) {
        this(context, attrs, 0);
    }

    public TitleBar(Context context, AttributeSet attrs, int defStyle) {
        super(context, attrs, defStyle);
        initView(context);
    }

    public void initView(Context context) {

        mContext  = (BaseActivity) getContext();
        View view = View.inflate(context, R.layout.activity_base_titlebar, null);

        mBack = (ImageView) view.findViewById(R.id.iv_base_back);
        mTitle = (TextView) view.findViewById(R.id.tv_base_title);
        mBack.setOnClickListener(this);
        mTitle.setOnClickListener(this);

        LayoutParams flp = new LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.WRAP_CONTENT);
        addView(view, flp);
    }


    /**
     * 中间控件文本内容
     */
    public void setTitle(String title) {
        if (mTitle != null) {
            mTitle.setText(title);
        }
    }


    /**
     * 控件的点击情况，分别去调用BaseActivity的方法，
     * 基类被重写后将在子类中调用这些方法</span>
     */
    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.iv_base_back:   //返回按钮
                mContext.finishActivity();
                break;
            case R.id.tv_base_title:  //标题的点击事件
                mContext.onTitleClick();
                break;
            default:
                break;
        }
    }
}

